package com.commerceerp.system.api.ebay;

import com.ebay.sdk.ApiAccount;
import com.ebay.sdk.ApiContext;
import com.ebay.sdk.ApiCredential;
import com.ebay.sdk.call.AddItemCall;
import com.ebay.sdk.util.eBayUtil;
import com.ebay.soap.eBLBaseComponents.*;

import java.io.IOException;

/**
 * Created by Administrator on 2018/9/29 0029.
 */
public class ApplicationAddItem {

    private static String ebayAppID = "feiwei-erp-PRD-38bab2703-5a349e0c";
    private static String ebayCertID = "29e02db9-edc9-4af8-8257-c00bbcba5888";
    private static String ebayDevID = "PRD-8bab27031b1a-b995-407c-96a4-6386";

    private static ApiContext getApiContext(String token) throws IOException {

        ApiContext apiContext = new ApiContext();
        ApiAccount apiAccount = new ApiAccount();
        apiAccount.setApplication(ebayAppID);
        apiAccount.setCertificate(ebayCertID);
        apiAccount.setDeveloper(ebayDevID);
        apiContext.getApiCredential().setApiAccount(apiAccount);
        ApiCredential cred = apiContext.getApiCredential();
        cred.seteBayToken(token);
        apiContext.setApiServerUrl("https://api.ebay.com/wsapi");
        apiContext.setEpsServerUrl("https://api.ebay.com/ws/api.dll");    // 这个要设置，不然会出错
        return apiContext;
    }

    private static ItemType builItem() throws IOException {
        ItemType item = new ItemType();

        // 项目标题
        item.setTitle("测试商品");

        // 画廊图片
        String[] paths = {"https://s3m.mediav.com/galileo/544440-ad47f6e778f9fdea0cb7cd8aec67b0eb.jpg"};
        PictureDetailsType pic = new PictureDetailsType();
        pic.setPictureURL(paths);
        //指定画廊图片
        pic.setGalleryType(GalleryTypeCodeType.GALLERY);
        item.setPictureDetails(pic);

        // 项目SKU
        item.setSKU("T000001");

        // 项目描述
        item.setDescription("描述商品");

        // 类型
        item.setListingType(ListingTypeCodeType.CHINESE);

        // 货币
        item.setCurrency(CurrencyCodeType.USD);
        AmountType amount = new AmountType();
        amount.setValue(Double.valueOf("0.01"));
        item.setStartPrice(amount);

        //上市时间
        item.setListingDuration(ListingDurationCodeType.DAYS_3.value());

        //  项目地点和国家
        item.setLocation("地点位置");
        item.setCountry(CountryCodeType.US);

        //  分类
        CategoryType cat = new CategoryType();
        cat.setCategoryID("166672");
        item.setPrimaryCategory(cat);

        // 库存数量
        item.setQuantity(new Integer(1));

        // 付款方式
        item.setPaymentMethods(new BuyerPaymentMethodCodeType[]{BuyerPaymentMethodCodeType.PAY_PAL});
        //设置 setPayPal 付款方式
        item.setPayPalEmailAddress("888@yirose.com");

        //  item 状态, 全新(1000)
        item.setConditionID(1000);

        // 需要处理时间
        item.setDispatchTimeMax(Integer.valueOf(1));

        // 运输细节
        item.setShippingDetails(buildShippingDetails());

        // 退货政策
        ReturnPolicyType returnPolicy = new ReturnPolicyType();
        returnPolicy.setReturnsAcceptedOption("ReturnsAccepted");
        item.setReturnPolicy(returnPolicy);

        return item;
    }

    /**
     * 构建产品运输细节
     * @return ShippingDetailsType 对象
     */
    private static ShippingDetailsType buildShippingDetails() {

        // 运输细节
        ShippingDetailsType sd =new ShippingDetailsType();

        sd.setApplyShippingDiscount(new Boolean(true));
        AmountType amount =new AmountType();
        amount.setValue(2.8);
        sd.setPaymentInstructions("eBay Java SDK 测试指令.");

        // 选择航运类型和航运服务
        sd.setShippingType(ShippingTypeCodeType.FLAT);
        ShippingServiceOptionsType shippingOptions  = new ShippingServiceOptionsType();
        shippingOptions.setShippingService(ShippingServiceCodeType.SHIPPING_METHOD_STANDARD.value());

        //amount = new AmountType();
        amount.setValue(2.0);
        shippingOptions.setShippingServiceAdditionalCost(amount);

        //amount  = new AmountType();
        amount.setValue(10);
        shippingOptions.setShippingServiceCost(amount);
        shippingOptions.setShippingServicePriority(new Integer(1));

        //amount  = new AmountType();
        amount.setValue(1);
        shippingOptions.setShippingInsuranceCost(amount);

        sd.setShippingServiceOptions(new ShippingServiceOptionsType[]{shippingOptions});

        return sd;
    }

    public static void main(String[] args) {
        try {

            System.out.println(" ");
            System.out.println("+++++++++++++++++++++++++++++++++++++++ ");
            System.out.println("+ 欢迎使用eBay API  JAVA实例 + ");
            System.out.println("+  - ConsoleAddItem   + ");
            System.out.println("+++++++++++++++++++++++++++++++++++++++ ");
            System.out.println(" ");

            // [Step 1] eBay ApiContext 对象初始化
            System.out.println("==== [1] 帐户信息  ====");
            ApiContext apiContext = getApiContext("AgAAAA**AQAAAA**aAAAAA**8N9mWw**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6ACloqpCpKKqQmdj6x9nY+seQ**E3MEAA**AAMAAA**X9umzl6LPmWU1f2ap8pQ6YW2MwwTsR/dMkPnJ8qussCFo7iOuSlUg66mWvqTqiEElTSf99SfbEnR8pLlIPj7YQxCSyuKDiOAXBRYED5RMBAcYIg9yUEot0Z+u51KX8F1/q334HZT5KW5o0WCikFiTekieEVTe75k//yWrVYGyhCVo3WrtJzjc1YBqoLGHuoadd8Bs5XUBALrfD/6Cexs5f7m4RNNMoKbKOQ7pILH7OZA3aXB7qq1aMsvBvuksTds2lKhMJDO5ura3D1EWeSYSXzT2yHU71jJHIzB4acHL02U5vkj7b0ZBtT83OMflL9KkCAjvw8YZUYDC1YaZp3oktZHSQERXPdjJhJtYPPLjkn8NmjP5bDI4FvlhtuUsx/ZJodsG3IERErlLa4KoY8wpe4nBjaGLGcFcjWC4ia0X8OIaGaKs/8bBKr4cbSU5Iimic19dgTzVa9IDGUsanEFDzb2xG/Iwt0IwAZFyUsLELU9UWYhAKS/OOlG0RUmykFkUMRPyEro7Se7aOYXuJX9dxba7kniLx9/WVjindrSFf+e5fhsF/11Yy+zpSBsRCrXYo7BBjpc3KLqhhQJE4zkt6OJtmTT3vNT+ra2+U98yVFxgxSselzjPcWQjs4dF8f1Dt0pRvo4RgK0hPI3K1dtTcoDcmQpKGcm1bMIAIDKuKSrMVyeWbmZmxSJ2ohLF8+6Gtx1c0nk6qLuNMLppGHf/trjDAquSblmF75T4L8G+ZAyl1B2ybi6WdZDATDeYxz+");

            // [Step 2] 创建新的 item 对象
            System.out.println("==== [2] Item信息  ====");
            ItemType item = builItem();

            // [Step 3] 创建调用对象并执行调用
            System.out.println("==== [3] 执行API调用  ====");
            System.out.println("开始调用eBay API，请稍候…  ");
            AddItemCall api = new AddItemCall(apiContext);
            api.setItem(item);
            FeesType fees = api.addItem();
            System.out.println("调用eBay API结束，显示调用结果…");
            System.out.println();

            // [Step 4] 显示结果
            System.out.println("Listing已成功发布！");

            double listingFee = eBayUtil.findFeeByName(fees.getFee(), "ListingFee").getFee().getValue();
            System.out.println("Listing 费用是:" + new Double(listingFee).toString());
            System.out.println("Listed Item ID:" + item.getItemID());

        } catch (Exception e) {
            System.out.println("错误！没有发布Listing");
            e.printStackTrace();
        }
    }

}
